//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.fox.redisbase;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public static final long DEFAULT_EXPIRE = 86400L;
    public static final long NOT_EXPIRE = -1L;

    public RedisServiceImpl() {
    }

    public boolean existsKey(String key) {
        return this.redisTemplate.hasKey(key).booleanValue();
    }

    public void renameKey(String oldKey, String newKey) {
        this.redisTemplate.rename(oldKey, newKey);
    }

    public boolean renameKeyNotExist(String oldKey, String newKey) {
        return this.redisTemplate.renameIfAbsent(oldKey, newKey).booleanValue();
    }

    public void deleteKey(String key) {
        this.redisTemplate.delete(key);
    }

    public void deleteKey(String... keys) {
        Set<String> kSet = (Set)Stream.of(keys).map((k) -> {
            return k;
        }).collect(Collectors.toSet());
        this.redisTemplate.delete(kSet);
    }

    public void deleteKey(Collection<String> keys) {
        Set<String> kSet = (Set)keys.stream().map((k) -> {
            return k;
        }).collect(Collectors.toSet());
        this.redisTemplate.delete(kSet);
    }

    public void expireKey(String key, long time, TimeUnit timeUnit) {
        this.redisTemplate.expire(key, time, timeUnit);
    }

    public void expireKeyAt(String key, Date date) {
        this.redisTemplate.expireAt(key, date);
    }

    public long getKeyExpire(String key, TimeUnit timeUnit) {
        return this.redisTemplate.getExpire(key, timeUnit).longValue();
    }

    public void persistKey(String key) {
        this.redisTemplate.persist(key);
    }

    public void set(String key, Object value) {
        this.redisTemplate.opsForValue().set(key, value);
    }

    public Object get(String key) {
        return this.redisTemplate.opsForValue().get(key);
    }
}
